package org.smartjava.concurrent.juc.aqs;

import org.smartjava.util.PrintUtil;
import org.smartjava.util.ThreadUtil;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) throws InterruptedException {
        //学生数量
        int students = 9;
        //初始化减法计数器为当前学生数量
        CountDownLatch countDownLatch = new CountDownLatch(students);
        for (int i = 0; i < students; i++) {
            new Thread(() -> {
                //模拟做题,时间有快有慢
                ThreadUtil.sleepRandom(3);
                PrintUtil.print(ThreadUtil.threadName(), "完成考试交卷");
                //完成了减法计数器减1
                countDownLatch.countDown();
            }, "同学-" + i).start();
        }
        countDownLatch.await();
        System.out.println("老师收齐试卷离开教室");
    }
}
